package com.xinqi.modules.stats.studyanalysis.service.impl;

import com.xinqi.modules.course.clazz.client.ClassStudentClient;
import com.xinqi.modules.stats.studyanalysis.domain.CourseStudentAnalysisEntity;
import com.xinqi.modules.stats.studyanalysis.mapper.CourseStudentAnalysisMapper;
import com.xinqi.modules.stats.studyanalysis.service.CourseStudentAnalysisService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 异步添加数据
 * @author dzy
 */
@Service("asyncStudentAnalysisService")
@RequiredArgsConstructor
public class AsyncStudentAnalysisService {

    private final CourseStudentAnalysisMapper courseStudentAnalysisMapper;

    @Lazy
    private final CourseStudentAnalysisService courseStudentAnalysisService;

    @Lazy
    private final ClassStudentClient classStudentClient;

    private final ThreadPoolTaskExecutor threadPoolTaskExecutor;

    public void asyncStudentAnalysis(Long classId) {
        List<CourseStudentAnalysisEntity> list = this.calcStudentAnalysis(classId);
        if (CollectionUtils.isNotEmpty(list) && list.size() > 0) {
            courseStudentAnalysisService.saveBatch(list);
        }
    }

    /**
     * 计算学生学情分析数据
     */
    private List<CourseStudentAnalysisEntity> calcStudentAnalysis(Long classId) {

        Map<Long, CourseStudentAnalysisEntity> map = new TreeMap<>();

        List<CourseStudentAnalysisEntity>[] list = threadStudentAnalysis(classId);

        // 互动课件赋值
        if (CollectionUtils.isNotEmpty(list[0])) {
            list[0].forEach(item -> {
                if (Objects.nonNull(item.getUserId())) {
                    CourseStudentAnalysisEntity entity = new CourseStudentAnalysisEntity();
                    entity.setClassId(classId);
                    entity.setUserId(item.getUserId());
                    entity.setInteractieCoursePublishNum(item.getInteractieCoursePublishNum());
                    entity.setInteractieCourseFinishedNum(item.getInteractieCourseFinishedNum());
                    map.put(item.getUserId(), entity);
                }
            });
        }

        // 资料赋值
        if (CollectionUtils.isNotEmpty(list[1])) {
            list[1].forEach(item -> {
                if (Objects.nonNull(item.getUserId())) {
                    CourseStudentAnalysisEntity entity = map.get(item.getUserId());
                    if (Objects.isNull(entity)) {
                        entity = new CourseStudentAnalysisEntity();
                        entity.setClassId(classId);
                        entity.setUserId(item.getUserId());
                        entity.setResoucePublishNum(item.getResoucePublishNum());
                        entity.setResouceFinishedNum(item.getResouceFinishedNum());
                        map.put(item.getUserId(), entity);
                    } else {
                        entity.setResoucePublishNum(item.getResoucePublishNum());
                        entity.setResouceFinishedNum(item.getResouceFinishedNum());
                        map.put(item.getUserId(), entity);
                    }
                }
            });
        }

        // 作业赋值
        if (CollectionUtils.isNotEmpty(list[2])) {
            list[2].forEach(item -> {
                if (Objects.nonNull(item.getUserId())) {
                    CourseStudentAnalysisEntity entity =  map.get(item.getUserId());
                    if (Objects.isNull(entity)) {
                        entity = new CourseStudentAnalysisEntity();
                        entity.setClassId(classId);
                        entity.setUserId(item.getUserId());
                        entity.setHomeworkPublishNum(item.getHomeworkPublishNum());
                        entity.setHomeworkFinishedNum(item.getHomeworkFinishedNum());
                        map.put(item.getUserId(), entity);
                    } else {
                        entity.setHomeworkPublishNum(item.getHomeworkPublishNum());
                        entity.setHomeworkFinishedNum(item.getHomeworkFinishedNum());
                        map.put(item.getUserId(), entity);
                    }
                }
            });
        }

        // 测评赋值
        if (CollectionUtils.isNotEmpty(list[3])) {
            list[3].forEach(item -> {
                if (Objects.nonNull(item.getUserId())) {
                    CourseStudentAnalysisEntity entity = map.get(item.getUserId());
                    if (Objects.isNull(entity)) {
                        entity = new CourseStudentAnalysisEntity();
                        entity.setClassId(classId);
                        entity.setUserId(item.getUserId());
                        entity.setExamPublishNum(item.getExamPublishNum());
                        entity.setExamFinishedNum(item.getExamFinishedNum());
                        map.put(item.getUserId(), entity);
                    } else {
                        entity.setExamPublishNum(item.getExamPublishNum());
                        entity.setExamFinishedNum(item.getExamFinishedNum());
                        map.put(item.getUserId(), entity);
                    }
                }
            });
        }

        // 讨论赋值
        if (CollectionUtils.isNotEmpty(list[4])) {
            list[4].forEach(item -> {
                if (Objects.nonNull(item.getUserId())) {
                    CourseStudentAnalysisEntity entity = map.get(item.getUserId());
                    if (Objects.isNull(entity)) {
                        entity = new CourseStudentAnalysisEntity();
                        entity.setClassId(classId);
                        entity.setUserId(item.getUserId());
                        entity.setDiscussPublishNum(item.getDiscussPublishNum());
                        entity.setDiscussFinishedNum(item.getDiscussFinishedNum());
                        map.put(item.getUserId(), entity);
                    } else {
                        entity.setDiscussPublishNum(item.getDiscussPublishNum());
                        entity.setDiscussFinishedNum(item.getDiscussFinishedNum());
                        map.put(item.getUserId(), entity);
                    }
                }
            });
        }

        // 互动视频赋值
        if (CollectionUtils.isNotEmpty(list[5])) {
            list[5].forEach(item -> {
                if (Objects.nonNull(item.getUserId())) {
                    CourseStudentAnalysisEntity entity = map.get(item.getUserId());
                    if (Objects.isNull(entity)) {
                        entity = new CourseStudentAnalysisEntity();
                        entity.setClassId(classId);
                        entity.setUserId(item.getUserId());
                        entity.setInteractieVideoPublishNum(item.getInteractieVideoPublishNum());
                        entity.setInteractieVideoFinishedNum(item.getInteractieVideoFinishedNum());
                        map.put(item.getUserId(), entity);
                    } else {
                        entity.setInteractieVideoPublishNum(item.getInteractieVideoPublishNum());
                        entity.setInteractieVideoFinishedNum(item.getInteractieVideoFinishedNum());
                        map.put(item.getUserId(), entity);
                    }
                }
            });
        }

        // 公告赋值
        if (CollectionUtils.isNotEmpty(list[6])) {
            list[6].forEach(item -> {
                if (Objects.nonNull(item.getUserId())) {
                    CourseStudentAnalysisEntity entity = map.get(item.getUserId());
                    if (Objects.isNull(entity)) {
                        entity = new CourseStudentAnalysisEntity();
                        entity.setClassId(classId);
                        entity.setUserId(item.getUserId());
                        entity.setNoticePublishNum(item.getNoticePublishNum());
                        entity.setNoticeFinishedNum(item.getNoticeFinishedNum());
                        map.put(item.getUserId(), entity);
                    } else {
                        entity.setNoticePublishNum(item.getNoticePublishNum());
                        entity.setNoticeFinishedNum(item.getNoticeFinishedNum());
                        map.put(item.getUserId(), entity);
                    }
                }
            });
        }

        // 考勤赋值
        if (CollectionUtils.isNotEmpty(list[7])) {
            list[7].forEach(item -> {
                if (Objects.nonNull(item.getUserId())) {
                    CourseStudentAnalysisEntity entity = map.get(item.getUserId());
                    if (Objects.isNull(entity)) {
                        entity = new CourseStudentAnalysisEntity();
                        entity.setClassId(classId);
                        entity.setUserId(item.getUserId());
                        entity.setAttendancePublishNum(item.getAttendancePublishNum());
                        entity.setAttendanceFinishedNum(item.getAttendanceFinishedNum());
                        map.put(item.getUserId(), entity);
                    } else {
                        entity.setAttendancePublishNum(item.getAttendancePublishNum());
                        entity.setAttendanceFinishedNum(item.getAttendanceFinishedNum());
                        map.put(item.getUserId(), entity);
                    }
                }
            });
        }

        List<CourseStudentAnalysisEntity> analysisList = new ArrayList<>();
        // 遍历有序 Map，结果将按键升序输出
        for (Map.Entry<Long, CourseStudentAnalysisEntity> entry : map.entrySet()) {
            analysisList.add(entry.getValue());
        }
        return analysisList;
    }

    /**
     * 异步线程执行数据采集
     */
    private List<CourseStudentAnalysisEntity>[] threadStudentAnalysis(Long classId) {

        List<CourseStudentAnalysisEntity>[] studentAnalysisList = new List[8];

        // 获取互动课件信息
        studentAnalysisList[0] = courseStudentAnalysisMapper.courseStudentCourseWareAnalysisList(classId);

        // 获取资料信息
        studentAnalysisList[1] = courseStudentAnalysisMapper.courseStudentDataAnalysisList(classId);

        // 获取作业信息
        studentAnalysisList[2] = courseStudentAnalysisMapper.courseStudentHomeworkAnalysisList(classId);

        // 获取测评信息
        studentAnalysisList[3] = courseStudentAnalysisMapper.courseStudentExamAnalysisList(classId);

        // 获取讨论信息
        studentAnalysisList[4] = courseStudentAnalysisMapper.courseStudentDiscussAnalysisList(classId);

        // 获取互动视频信息
        studentAnalysisList[5] = courseStudentAnalysisMapper.courseStudentVideoAnalysisList(classId);

        // 获取公告信息
        studentAnalysisList[6] = courseStudentAnalysisMapper.courseStudentNoticeAnalysisList(classId);

        // 获取考勤信息
        studentAnalysisList[7] = courseStudentAnalysisMapper.courseStudentAttendanceAnalysisList(classId);

        return studentAnalysisList;
    }


}
